package com.qys.livesMall.shop.controller.sys;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qys.livesMall.common.exception.QysException;
import com.qys.livesMall.common.result.R;
import com.qys.livesMall.common.utils.MessageUtils;
import com.qys.livesMall.shop.dao.H2ShopBaseDao;
import com.qys.livesMall.shop.modal.dto.SeckillDto;
import com.qys.livesMall.shop.modal.dto.ShopProductDto;
import com.qys.livesMall.shop.modal.entity.H2ShopBase;
import com.qys.livesMall.shop.modal.entity.SeckillActivity;
import com.qys.livesMall.shop.modal.entity.SeckillGoods;
import com.qys.livesMall.shop.modal.query.QuerySeckill;
import com.qys.livesMall.shop.modal.query.QuerySeckillShopGoods;
import com.qys.livesMall.shop.modal.vo.Seckill;
import com.qys.livesMall.shop.modal.vo.SeckillGoodsVO;
import com.qys.livesMall.shop.modal.vo.SeckillVo;
import com.qys.livesMall.shop.modal.vo.ShopGoodsSkuVO;
import com.qys.livesMall.shop.service.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author gmx
 * @since 2023-07-03
 */
@RestController
@RequestMapping("/sys/seckillActivity")
@Valid
@Tag(name = "sys-秒杀活动",description = "sys-秒杀活动")
public class SysSeckillActivityController {
    @Resource
    private SeckillActivityService seckillActivityService;
    @Resource
    private H2ShopBaseDao h2ShopBaseDao;
    @Operation(summary = "添加秒杀活动")
    @PostMapping("/add")
    public R add(@Validated @RequestBody SeckillDto seckillDto){
        try {
            return seckillActivityService.addSeckill(seckillDto);

        }catch (QysException e){
            return R.fail(e.getMessage());
        }
    }
    @Operation(summary = "获取商品列表",deprecated = true)
    @PostMapping("/goodsList")
    public R<Page<ShopProductDto>> goodsList(@RequestBody @Validated QuerySeckillShopGoods seckillShopGoods){
        return seckillActivityService.goodsList(seckillShopGoods);
    }

    @Operation(summary = "获取商品搜索列表")
    @PostMapping("/goods/list")
    public R<Page<ShopGoodsSkuVO>> listSearchGoods(@RequestBody @Validated QuerySeckillShopGoods seckillShopGoods){
        return seckillActivityService.listSearchGoods(seckillShopGoods);
    }


    @Operation(summary = "根据商品id获取商品sku")
    @GetMapping("/skuList/{goodsId}")
    public R<List<ShopGoodsSkuVO>> getSkuList(@PathVariable("goodsId") Long goodsId){
        return seckillActivityService.getSkuList(goodsId);
    }


    @Operation(summary = "获取商家列表")
    @GetMapping("/getShopList/{skuId}")
    public R<List<H2ShopBase>> getShopList(@PathVariable Long skuId){
        return R.ok(seckillActivityService.getShopList(skuId));
    }
    @Operation(summary = "获取活动列表")
    @PostMapping("/list")
    public R<Page<SeckillVo>> List(@RequestBody QuerySeckill querySeckill){
        return seckillActivityService.QueryList(querySeckill);
    }
    @Operation(summary = "停止活动")
    @GetMapping("/stop/{id}")
    public R stop(@PathVariable Long id){
        long now = new Date().getTime();
        SeckillActivity activity = seckillActivityService.getById(id);
        int i = Long.compare(activity.getStartTime(), now);
        if (i>0){
            return R.fail(MessageUtils.message("seckill.not.started"));
        }
        if (i<=0){
            int g = Long.compare(activity.getEndTime(), now);
            if (g<0){
                return R.fail(MessageUtils.message("seckill.is.end"));
            }
        }
        activity.setEndTime(now);

        return seckillActivityService.updateById(activity)? R.ok():R.fail(MessageUtils.message("system.error.seckill"));
    }

    @Operation(summary = "获取详情")
    @GetMapping("/{id}")
    public R<Seckill> get(@PathVariable Long id){
        SeckillActivity activity = seckillActivityService.getById(id);
        if (ObjectUtils.isEmpty(activity))return R.fail(MessageUtils.message("no.such.activity"));
        Seckill seckill = new Seckill();
        BeanUtil.copyProperties(activity,seckill);
        List<SeckillGoods> goodsList = seckillActivityService.getGoods(id);
        if (ObjectUtils.isEmpty(goodsList))return R.fail(MessageUtils.message("no.such.activity.goods"));
        seckill.setNum(goodsList.get(0).getNum());
        List<SeckillGoodsVO> goodsVOList = new ArrayList<>();
        for (SeckillGoods seckillGoods : goodsList) {
            SeckillGoodsVO goodsVO = new SeckillGoodsVO();
            BeanUtil.copyProperties(seckillGoods,goodsVO);
            List<String> shopIds = seckillActivityService.makeJson(seckillGoods.getShopId());
            if (ObjectUtils.isEmpty(shopIds)){
                return R.fail(MessageUtils.message("system.error"));
            }
            List<H2ShopBase> shopBase = h2ShopBaseDao.listByIds(shopIds);
            goodsVO.setShop(shopBase);
            goodsVOList.add(goodsVO);
        }
        seckill.setSeckillGoods(goodsVOList);
        return R.ok(seckill);
    }

}
